package com.migo.api;

import com.alibaba.fastjson.JSONObject;
import com.migo.annotation.IgnoreAuth;
import com.migo.entity.TeachingVideoEntity;
import com.migo.service.TeachingVideoService;
import com.migo.utils.JsonUtils;
import com.migo.utils.R;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * Created by Wes on 2019/1/16.
 */
@RestController
@RequestMapping("/api")
public class ApiTeachingVideoController extends ApiBaseController{


    @Autowired
    private TeachingVideoService teachingVideoService;


    @Autowired
    private StringRedisTemplate redisTemplate;

    /**
     * 查询产品的教学视频
     */
    @IgnoreAuth
    @RequestMapping("/queryTeachingVideoList")
    public R queryTeachingVideoList(String proId, String language) throws Exception {
        Map<String,Object> paramMap = new HashedMap();
        paramMap.put("proId",proId);
        paramMap.put("language",language);
        String result = "";
        //缓存处理开始
        String key = "queryTeachingVideoList_" + proId+ "_" + language;
        boolean exisKey = redisTemplate.hasKey(key);
        if (exisKey) {
            result = redisTemplate.boundValueOps(key).get();
            if (result == null || result.isEmpty()) {
                result = JsonUtils.getJSONString(teachingVideoService.queryList(paramMap));
                redisTemplate.boundValueOps(key).set(result);
            }
        } else {
            result = JsonUtils.getJSONString(teachingVideoService.queryList(paramMap));
            redisTemplate.boundValueOps(key).set(result);
        }
        List<TeachingVideoEntity> newList= JSONObject.parseArray(result, TeachingVideoEntity.class);
        return R.ok().put("data",newList);
    }

    /**
     * 查询教学视频详情
     */
    @IgnoreAuth
    @RequestMapping("/queryTeachingVideoDetail")
    public R queryTeachingVideoDetail(String id, String language) throws Exception {
        String result = "";
        //缓存处理开始
        String key = "queryTeachingVideoDetail_" + id+ "_" + language;
        boolean exisKey = redisTemplate.hasKey(key);
        if (exisKey) {
            result = redisTemplate.boundValueOps(key).get();
            if (result == null || result.isEmpty()) {
                result = JsonUtils.getJSONString(teachingVideoService.queryObject(Integer.parseInt(id)));
                redisTemplate.boundValueOps(key).set(result);
            }
        } else {
            result = JsonUtils.getJSONString(teachingVideoService.queryObject(Integer.parseInt(id)));
            redisTemplate.boundValueOps(key).set(result);
        }
        TeachingVideoEntity teachingVideoEntity= JSONObject.parseObject(result, TeachingVideoEntity.class);
        return R.ok().put("data",teachingVideoEntity);
    }



}
